const MyPromise = require('./MyPromise');

let promise = new MyPromise((resolve, reject) => {
    // setTimeout(() => {
    //     resolve('成功...')
       
    // }, 2000);
    // throw new Error('executor error')
    // resolve('成功')
     reject('失败')
})

// promise.then().then().then(value => console.log(value),reason => console.log(reason))




// function other() {
//     return new MyPromise((resolve, reject) => {
//         // resolve('other')
//         reject('other 失败')
//     })
// }

// promise.then(value => {
//     console.log(value)
//     // throw new Error('then error')
//     return 'aaa'
// },reason => {
//     console.log(reason)
//     return 10000
// })

// .then(value => {
//     console.log(value)
// }, reason => {
//     console.log(reason)
// })


// let p1 = promise.then(value => {
//     console.log(1)
//     console.log(value)
//     return p1
// })




// p1.then(value => {
//     console.log(value)
// },reason => {
//     console.log(reason.message)
// })


function p1 () {
    return new MyPromise(function (resolve, reject) {
      setTimeout(function () {
        resolve('p1')
      }, 2000)
    //   resolve('p1')
    })
  }
function p2 () {
    return new MyPromise(function (resolve, reject) {
        reject('p2 失败')
        // resolve('p2 成功');  
    })
}

p2()
    .then(value => console.log(value))
    .catch(reason => console.log(reason))


// MyPromise.all(['a','b',p1(),p2(),'c']).then(function(result) {
//     console.log(result)
// })

// MyPromise.resolve(100).then(value => console.log(value));
// MyPromise.resolve(p1()).then(value => console.log(value));

// p2().finally(() => {
//     console.log('finally')
//     return p1();
// }).then(value => {
//     console.log(value)
// }, reason => {
//     console.log(reason)
// })